﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using EnvDTE;
using EnvDTE100;

namespace Artisteer_Project_Converter.VisualStudioApi
{
    public class VsSolution
    {
        private Solution4 solution = null;

        /// <summary>
        /// Create solution instance
        /// </summary>
        /// <param name="solution">Solution object</param>
        internal VsSolution(Solution4 solution)
        {
            this.solution = solution;
        }

        /// <summary>
        /// Create a new solution
        /// </summary>
        /// <param name="path">Target path</param>
        /// <param name="name">Solution name</param>
        internal void CreateSolution(string path, string name)
        {
            if (!Directory.Exists(path)) 
                Directory.CreateDirectory(path);
            
            solution.Create(path, name);
            solution.SaveAs(System.IO.Path.Combine(path, name + ".sln"));
        }

        /// <summary>
        /// Create an new project
        /// </summary>
        /// <param name="template">Template name</param>
        /// <param name="language">Programming language</param>
        /// <param name="path">Target path</param>
        /// <param name="name">Project name</param>
        /// <returns>Project</returns>
        public VsProject CreateProject(string template, string language, string path, string name)
        {
            return CreateProject(solution.GetProjectTemplate(template, language), path, name);
        }

        /// <summary>
        /// Create an new project
        /// </summary>
        /// <param name="template">Template path</param>
        /// <param name="path">Target path</param>
        /// <param name="name">Project name</param>
        /// <returns>Project</returns>
        public VsProject CreateProject(string template, string path, string name)
        {
            solution.AddFromTemplate(template, path, name);
            
            return new VsProject(solution.Projects.Item(solution.Projects.Count));
        }

        /// <summary>
        /// Save solution as
        /// </summary>
        public void SaveAs(string filename)
        {
            solution.SaveAs(filename);
        }
    }
}
